package com.test.copy_file;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;

public class CopyTxt {
    private static final String INPUT_PATH = "C:\\Users\\Administrator\\Desktop\\2015";
    private static final String OUTPUT_PATH = "C:\\Users\\Administrator\\Desktop\\连岳2015.txt";


    public static void main(String[] args) {
        File file=new File(INPUT_PATH);
        showDirectory(file);
    }

    public static void showDirectory(File file) {
        int count = 0;
        File[] files = file.listFiles();
        //按日期排序
        Arrays.sort(files,new Comparator< File>() {
            public int compare(File f1, File f2) {
                long diff = f1.lastModified() - f2.lastModified();
                if (diff > 0)
                    return 1;
                else if (diff == 0)
                    return 0;
                else
                    return -1;
            }
        });
        for(File a:files) {
            if(!a.isDirectory() && a.getAbsolutePath().endsWith("txt")){
                //System.out.println(a.getAbsolutePath());
                count++;
                copyFile(a.getAbsolutePath());
            }
            if(a.isDirectory()) {
                showDirectory(a);
            }
        }
        System.out.println("总共"+count);
    }

    public static void copyFile(String absolutePath) {
        File oldFile =new File(absolutePath);
        try {
            BufferedReader br = new BufferedReader(new FileReader(oldFile));
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(oldFile),"UTF-8"));
            //一个文件开始加个标记行
            writeToFile("======"+absolutePath+" start========");
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                writeToFile(s);
            }
            //一个文件结束加个标记行
            writeToFile("  " + System.lineSeparator());
            br.close();
        } catch (Exception e) {

        }
    }

    /*
	  * 写文件
	  * 末尾续接
	  * FileWriter
	  * 先写在缓存中,需要flush
	  */
    public static void writeToFile(String str){
        FileWriter writer;
        try {
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            writer = new FileWriter(OUTPUT_PATH, true);
            writer.write(str + System.lineSeparator());
            writer.flush();//这一个比较重要，是清理缓存的过程，之后需要写入的信息被写到txt文件中
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //按日期排序
    public static File[] orderByDate(File file) {
        //File file = new File(fliePath);
        File[] fs = file.listFiles();
        Arrays.sort(fs,new Comparator< File>() {
            public int compare(File f1, File f2) {
                long diff = f1.lastModified() - f2.lastModified();
                if (diff > 0)
                    return 1;
                else if (diff == 0)
                    return 0;
                else
                    return -1;
            }
        });
        return fs;
    }
}